package com.contract.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.contract.dao.ContractDao;
import com.contract.dao.impl.ContractDaoImpl;
import com.contract.pojo.Markinfo;
import com.contract.pojo.Tradinfo;
import com.contract.service.ContractService;
import com.contract.utils.DBUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class ContractServiceImpl implements ContractService {

	private ContractDao contractDao = new ContractDaoImpl();
	
	@Override
	public List queryExpMonth() {
		Connection connection = null;
		List list = new ArrayList();
		try {
			connection = DBUtil.getConnection();
			
			 list = contractDao.queryExpMonth(connection);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DBUtil.closeConnection(connection);
		return list;
	}

	@Override
	public void addTadInfo(List<String> strlist) {
		
		Connection connection = null;
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		Calendar cal = Calendar.getInstance();
		String year =  String.valueOf(cal.get(Calendar.YEAR));
		String month =  String.valueOf(cal.get(Calendar.MONTH )+1);
			try {
				connection = DBUtil.getConnection();
				//删除原来表里面数据
				//contractDao.dropTradInfo(connection);
				
				for(int i=0;i<strlist.size();i++){
				
					String str = strlist.get(i);
					if(str!=null && str.indexOf("callback")!=-1){			
						String resuts = str.substring(9, str.length()-1);
						//Gson gson = new Gson();
						JsonObject obj = new JsonParser().parse(resuts).getAsJsonObject();
						JsonArray asJsonArray = obj.get("list").getAsJsonArray();
						
						for(JsonElement jsonElement : asJsonArray){
							Tradinfo tradinfo = new Tradinfo();
							String jsonarayy = jsonElement.toString();
							jsonarayy = jsonarayy.substring(1,jsonarayy.length()-1);
							String[] split = jsonarayy.split(",");
							String ticker = split[0];
							ticker = ticker.substring(1, ticker.length()-1).trim();
							
							tradinfo.setTickerSymbol(ticker);
							tradinfo.setLast(split[1]);
							tradinfo.setChgrat(split[2]);
							tradinfo.setPresetpx(split[3]);
							tradinfo.setExepx(split[4]);
							tradinfo.setAddyear(year);
							tradinfo.setAddmonth(month);
							tradinfo.setAddate(ts);
													
							//contractDao.addTadInfo(tradinfo,connection);
							contractDao.addTadInfoAll(tradinfo, connection);
						}
					}
				}
				connection.commit();
				DBUtil.closeConnection(connection);
			} catch (SQLException e) {
				try {
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
		
			
			
			
		
		

	}

	@Override
	public void addMarkInfo(String str) {

		Connection connection = null;	
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		Calendar cal = Calendar.getInstance();
		String year =  String.valueOf(cal.get(Calendar.YEAR));
		String month =  String.valueOf(cal.get(Calendar.MONTH )+1);
			try {
				connection = DBUtil.getConnection();
				
				//删除原来表里面数据
				contractDao.dropMarkInfo(connection);
				
				if(str!=null && str.indexOf("callback")!=-1){			
					String resuts = str.substring(9, str.length()-1);
					//Gson gson = new Gson();
					JsonObject obj = new JsonParser().parse(resuts).getAsJsonObject();
					JsonArray asJsonArray = obj.get("list").getAsJsonArray();
					
					for(JsonElement jsonElement : asJsonArray){
						Markinfo markinfo = new Markinfo();
						String jsonarayy = jsonElement.toString();
						jsonarayy = jsonarayy.substring(1,jsonarayy.length()-1);
						String[] split = jsonarayy.split(",");
						
						
						String code = split[0];
						code = code.substring(1, code.length()-1).trim();			 
						markinfo.setCode(code);
						String name = split[1];
						name = name.substring(1, name.length()-1).trim();	
						markinfo.setName(name);
						
						markinfo.setLast(split[2]);
						markinfo.setChange(split[3]);
						markinfo.setChgrate(split[4]);
						markinfo.setAmprate(split[5]);
						
						String volum = split[6];
						markinfo.setVolume(volum);
						
						markinfo.setAmount(split[7]);
						markinfo.setAddyear(year);
						markinfo.setAddmonth(month);
						markinfo.setAddate(ts);
						
						contractDao.addMarkInfo(markinfo,connection);
						
					}				
					
				}
				connection.commit();
				DBUtil.closeConnection(connection);
				
			} catch (SQLException e) {
				try {
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.printStackTrace();
			}
		
		
	}

}
